//
//  M4HiddenPointDataManager.swift
//  M4
//
//  Created by jie on 2025/3/5.
//


import Swift
import Foundation
import UIKit

class M4HiddenPointDataManager {
    
    enum M4HiddenPointDataType: Int {
        case Luanch = 12000
        case Login = 1000
        case Home = 2000
        case Personal = 3000
        case Contact = 4000
        case BankCard = 5000
        case IDCard = 6000
        case Face = 7000
        case ConfimLoan = 9000
        case PayDetail = 10000
        case PayZhanQi = 11000
        case creditInfoZhanQi = 13000

    }
    
    class func M4LuanckUploadHiddenPoint(inTime: Int) {
        var params: [String:Any] = [
            "FASdas" : 12000,
            "gasdqwr" : Int(Date().timeIntervalSince1970 * 1000),
            "asdFAFA" : M4UserDataManager.m4UserShare.M4Token,
            "DAS": "ESP",
           "fASw": M4UserDataManager.m4UserShare.M4Itemcode
        ]
        params["FFFqw"] = "52\(M4UserDataManager.m4UserShare.M4UserMobile)"
        params["FASDW"] = UIDevice.current.model
        params["GAGSqwg"] =  M4UserDataManager.m4UserShare.adjustAdid
        params["GASraa"] = M4UserDataManager.m4UserShare.M4appUserId
        params["GASdwrg"] = M4UserDataManager.m4UserShare.M4Version
        params["GASsgg"] = M4DeviceDataManager.m4DeviceShare.ips.first ?? ""
        params["GaGsss"] = M4DeviceDataManager.m4DeviceShare.m4XwMAiX
        params["GasfA"] = M4DeviceDataManager.m4DeviceShare.wifiName
        params["GAswrq"] = M4UserDataManager.m4UserShare.M4Latitude
        params["GgGGss"] = M4UserDataManager.m4UserShare.M4Longtitude
        params["fasdFA"] = ProcessInfo.processInfo.operatingSystemName()
        params["FASasf"] = ProcessInfo.processInfo.operatingSystemVersionString
        params["FASdqweA"] = inTime
        params["ASdqwrg"] = Int(Date().timeIntervalSince1970 * 1000) 
        
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4NewPointData, .post, params: params), false) {model in
            
        }
    }
    
    class func M4UploadHiddenPoint(type: M4HiddenPointDataType, paramsKey: String, params: [[String:Any]], complete: (() -> Void)? ) {
        var jsonString: String = ""
        do {
            let jsonData = try JSONSerialization.data(withJSONObject: params)
            jsonString = String(data: jsonData, encoding: .utf8) ?? ""
        } catch {
            print("JSON序列化失败: \(error)")
        }
        
        let p: [String:Any] = [
            "FASdas" : type.rawValue,
            paramsKey : jsonString,
            "gasdqwr" : Int(Date().timeIntervalSince1970 * 1000),
            "asdFAFA" : M4UserDataManager.m4UserShare.M4Token,
            "DAS": "ESP",
            "FFFqw": "52\(M4UserDataManager.m4UserShare.M4UserMobile)",

            "fASw": M4UserDataManager.m4UserShare.M4Itemcode,

        ]
        M4NetworkManager().M4Request(M4NetworkTarget.M4_targetType(.m4NewPointData, .post, params: p), false) {model in
            
        }
    }
}


import SystemConfiguration.CaptiveNetwork
import Network
extension UIDevice {
   class func getLocalIPAddresses() -> [String]? {
        var addresses = [String]()
        var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil

        if getifaddrs(&ifaddr) == 0 {
            var ptr = ifaddr
            while ptr != nil {
                let flags = Int32(ptr!.pointee.ifa_flags)
                var addr = ptr!.pointee.ifa_addr.pointee

                if (flags & (IFF_UP | IFF_RUNNING | IFF_LOOPBACK)) == (IFF_UP | IFF_RUNNING) {
                    if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) {
                        var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
                        if getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count), nil, socklen_t(0), NI_NUMERICHOST) == 0 {
                            let address = String(cString: hostname)
                            addresses.append(address)
                        }
                    }
                }
                ptr = ptr!.pointee.ifa_next
            }
            freeifaddrs(ifaddr)
        }

        return addresses.isEmpty ? nil : addresses
    }

    
   class func getWifiName() -> String? {
        var wifiName: String?
        guard let interfaces = CNCopySupportedInterfaces() as? [String], !interfaces.isEmpty else {
            return nil
        }
        if let interfaceName = interfaces.first {
            guard let networkInfo = CNCopyCurrentNetworkInfo(interfaceName as CFString) as? [String: Any],
                  let ssid = networkInfo[kCNNetworkInfoKeySSID as String] as? String else {
                return nil
            }
            wifiName = ssid
        }
        
        return wifiName
    }
}
